package com.yans.views
{
	import com.yans.maps.MapConfig;
	import com.yans.maps.vo.DragVo;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import mx.controls.Image;
	
	public class Item extends Sprite
	{
		
		public var id:int;
		public var type:int;
		
		private var content:Image;
		private var bitmapdata:BitmapData;
		private var sourcePath:String;
		private var pos:Array;
		private var selectSprite:Sprite;
		public var selectedHandler:Function;
		private var downPoint:Point;
		public var selected:Boolean;
		
		public var vo:DragVo;
		
		public function clone():Item{
			var _local1:Item = new Item();
			_local1.init(this.id, this.type, sourcePath, this.pos);
			_local1.selectedHandler = this.selectedHandler;
			return (_local1);
		}
		public function init(itemID:int, itemType:int, source:String, point:Array):void{
			content = new Image;
			vo = new DragVo;
			this.mouseChildren = false;
			vo.id = this.id = itemID;
			vo.type = this.type = itemType;
			//this.bitmapdata = bd;
			vo.path = sourcePath = source;
			this.pos = point;
			//var _local5:Bitmap = new Bitmap(bd);
			content.autoLoad = true;
			x = point[0];
			y = point[1];
//			addChild(_local5);
			content.source = source;
			content.addEventListener(Event.COMPLETE, function(e:Event):void {
				//bitmapdata = BitmapData(_local5.content); 
				addChild(content.content);
				content.content.x = width - content.content.width >> 1;
				content.content.y = height - content.content.height >> 1;
				var _local6:Shape = new Shape();
				_local6.graphics.lineStyle(2, 0xFF0000);
				_local6.graphics.beginFill(0xFF0000);
				_local6.graphics.drawCircle(0, 0, 3);
				_local6.graphics.endFill();
				addChild(_local6);
			});
			content.addEventListener(IOErrorEvent.IO_ERROR, function(e:IOErrorEvent):void {
				trace("加载出错", e.text);
			});
			content.load(source);
			this.selectSprite = new Sprite();
			addChild(this.selectSprite);
			this.addEventListener(MouseEvent.MOUSE_DOWN, this.onDown);
			this.addEventListener(MouseEvent.CLICK, this.onClick);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
		}
		
		public function onAddToStage(e:Event = null):void {
//			vo.targetMapX = x;
//			vo.targetMapY = y;
		}
		
		private function onDown(_arg1:MouseEvent):void{
			this.downPoint = new Point();
			if(!MapConfig.mousePoint) this.downPoint.x = _arg1.stageX; 
			else this.downPoint.x = MapConfig.mousePoint.x;
			if(!MapConfig.mousePoint) this.downPoint.y = _arg1.stageY;
			else this.downPoint.y = MapConfig.mousePoint.y;
		}
		private function onClick(_arg1:MouseEvent):void{
			if (this.downPoint != null)
			{
				if ((((Math.abs((this.downPoint.x - MapConfig.mousePoint.x)) > 10)) || ((Math.abs((this.downPoint.y - MapConfig.mousePoint.y)) > 10))))
				{
					return;
				};
			};
			if (this.selectedHandler != null)
			{
				if (_arg1.ctrlKey)
				{
					this.selectedHandler.apply(null, [this, false]);
				} else
				{
					this.selectedHandler.apply(null, [this]);
				};
			};
		}
		public function select(_arg1:Array):void{
			this.selected = true;
			if (_arg1.indexOf(this) == -1)
			{
				_arg1.push(this);
			};
			this.selectSprite.graphics.clear();
			this.selectSprite.graphics.lineStyle(2, 0xFF0000);
			this.selectSprite.graphics.beginFill(0xFFFFFF, 0);
			this.selectSprite.graphics.drawRect(-40, -30, 80, 60);
			this.selectSprite.graphics.endFill();
		}
		public function unselected(_arg1:Array):void{
			this.selected = false;
			if (_arg1.indexOf(this) != -1)
			{
				_arg1.splice(_arg1.indexOf(this), 1);
			};
			this.selectSprite.graphics.clear();
		}
	}
}